package Dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import Model1.payment;
import Model1.sellorder;

public class paymentDao {
	private static Connection conn;
	private static PreparedStatement ps;
	private static ResultSet rs;
	
	
	public static Boolean insert(payment pay) {
		if(pay==null)
			return false;
		conn = DatabaseConn.getConn();
		String sql = "insert into payment(payid,money,orderid,time,method) values(?,?,?,?,?)";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, pay.getPayid());
			ps.setFloat(2, pay.getMoney());
			ps.setString(3, pay.getOrderId());
			ps.setString(4, pay.getTime());
			ps.setString(5, pay.getMethod());
			int i = ps.executeUpdate();
			DatabaseConn.close(conn, ps, rs);
			return i==1;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	public payment SearchById(int payid) {
		if(payid<=0)
			return null;
		conn = DatabaseConn.getConn();
		payment pay = null;
		String sql = "select * from payment where payid like "+payid;
		try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while(rs.next()) {
				pay = new payment(rs.getString("payid"),rs.getFloat("money"),rs.getString("orderid"),rs.getString("time"),rs.getString("method"));
			}
			DatabaseConn.close(conn, ps, rs);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return pay;
	}
	
	public Boolean Delete(int payid) {
		if(payid<=0)
			return false;
		conn = DatabaseConn.getConn();
		String sql = "delete from payment where payid like "+payid;
		try {
			ps = conn.prepareStatement(sql);
			int i =ps.executeUpdate();
			DatabaseConn.close(conn, ps, rs);
			return i==1;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	public static String SearchMaxId() {
		
		conn = DatabaseConn.getConn();
		
		String payId="";
		String sql = "select MAX(payid) from payment";
		try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while(rs.next()) {
				 payId=rs.getString("MAX(payid)");
			}
			DatabaseConn.close(conn, ps, rs);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return  payId;
	}
	
public static double SearchbyTime(String time) {
		
		conn = DatabaseConn.getConn();
		
		double d=0;
		String sql = "select money FROM payment where  time like ?";
		try{ 
			ps = conn.prepareStatement(sql);
			ps.setString(1, time);
			rs = ps.executeQuery();
			while(rs.next()) {
				d+=rs.getDouble("money");
			}
			DatabaseConn.close(conn, ps, rs);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return  d;
	}
	
	public Boolean Update(payment pay) {
		conn = DatabaseConn.getConn();
		String sql = "update payment set money=?,orderid=?,time=?,method=? where payid=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setFloat(1, pay.getMoney());
			ps.setString(2, pay.getOrderId());
			ps.setString(3, pay.getTime());
			ps.setString(4, pay.getMethod());
			ps.setString(5, pay.getPayid());
			int i = ps.executeUpdate();
			DatabaseConn.close(conn, ps, rs);
			return i==1;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	public List<payment> findAll() {
		conn = DatabaseConn.getConn();
		List<payment> pay=new ArrayList<>();
		String sql = "select * from payment";
		try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while(rs.next()) {
				pay.add(new payment(rs.getString("payid"),rs.getFloat("money"),rs.getString("orderid"),rs.getString("time"),rs.getString("method")));
			}
			DatabaseConn.close(conn, ps, rs);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return pay;
	}
}